package com.hua.common.commonview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.hua.common.R;
import com.hua.common.commontools.AppUtils;
import com.hua.common.commontools.DipUtil;


/**
 * 可以拖动的view控件
 *
 * @author huaxiaolin_pc
 */
public class DragControl extends RelativeLayout {

	private ImageView img = null;
	private int endX = 0;
	private int endY = 0;
	private float x;
	private float y;
	private float mTouchStartX;
	private float mTouchStartY;
	/**
	 * 状态栏的高度
	 */
	private float top = 0f;
	/**
	 * 手机屏幕的宽度
	 */
	private float phoneWidth = 720f;
	/**
	 * 屏幕高度
	 */
	private float phoneHeight = 1280f;
	/**
	 * 可拖动控件的高度
	 */
	private float imageWH = DipUtil.Dp2px(getContext(), 50);
	/**
	 * 点击事件回掉接口
	 */
	private MyClicklistener clicklistener = null;
	/*
     * 上次的位置
     */
	private int lastEndX = 0;
	private int lastEndY = 0;

	public DragControl(Context context) {
		super(context);
		init();
	}

	public DragControl(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public DragControl(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		this.initViews();
	}

	private void initViews() {
		this.img = new ImageView(getContext());
		this.img.setImageResource(R.drawable.ic_launcher);
		this.img.setPadding(5, 5, 5, 5);
		this.addView(img);
		LayoutParams lp = (LayoutParams) this.img.getLayoutParams();
		lp.width = (int) imageWH;
		lp.height = (int) imageWH;
		lp.setMargins(0, 500, 0, 0);
		this.img.setLayoutParams(lp);
		this.img.setOnTouchListener(new ontouchListener());
	}

	/**
	 * 设置显示的图片
	 *
	 * @param url
	 */
	public void setImageUrl(String url) {
		if (img != null) {
//			ImageLoader.Danimlib.jarisplay(this.img, url, ImageUtil.NODEFAULT);
		}
	}

	/**
	 * 设置点击事件回掉接口
	 *
	 * @param myClicklistener
	 */
	public void setClickListenter(MyClicklistener myClicklistener) {
		this.clicklistener = myClicklistener;
	}

	/**
	 * 更新位置的
	 */
	private void updateViewPosition() {
		if (endY < 0) {
			endY = 0;
		}
		if (endX < 0) {
			endX = 0;
		}
		LayoutParams layoutparams = (LayoutParams) img.getLayoutParams();
		layoutparams.setMargins(endX, endY, 0, 0);
		img.setLayoutParams(layoutparams);
	}

	public void setImgLayout(){
		LayoutParams lp = (LayoutParams) this.img.getLayoutParams();
		lp.width = (int) imageWH;
		lp.height = (int) imageWH;
		endX=(int) (phoneWidth-imageWH);
		endY=500;
		lp.setMargins(endX, endY, 0, 0);
	}
	/**
	 * 设置状态栏的高度
	 *
	 * @param top
	 */
	public void setMyTop(float top) {
		this.top = top+ AppUtils.getStatusBarHeight();
	}

	/**
	 * 设置屏幕宽度
	 *
	 * @param phoneWidth
	 */
	public void setPhoneWidth(float phoneWidth) {
		this.phoneWidth = phoneWidth;
	}

	/**
	 * 设置屏幕高度
	 *
	 * @param phoneHeight
	 */
	public void setPhoneHeight(float phoneHeight) {
		this.phoneHeight = phoneHeight;
	}

	/**
	 * 设置可移动控件的宽高
	 *
	 * @param imageWH
	 */
	public void setImageWH(float imageWH) {
		this.imageWH = imageWH;
		LayoutParams lp = (LayoutParams) this.img.getLayoutParams();
		lp.width = (int) imageWH;
		lp.height = (int) imageWH;
		this.img.setLayoutParams(lp);
	}

	/**
	 * 开启动画
	 */
	private void startAnim() {
		ValueAnimator valueAnimator = null;
		if (endX > this.phoneWidth / 2) {
			valueAnimator = ValueAnimator.ofFloat(endX, this.phoneWidth
					- imageWH);
		} else {
			valueAnimator = ValueAnimator.ofFloat(endX, 0);
		}
		valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator valueAnimator) {
				float temp = (float) valueAnimator.getAnimatedValue();
				endX = (int) temp;
				lastEndX = endX;
				lastEndY = endY;
				updateViewPosition();
			}
		});
		valueAnimator.setDuration(100);
		valueAnimator.start();
	}

	public interface MyClicklistener {
		void click();
	}

	class ontouchListener implements OnTouchListener {
		@Override
		public boolean onTouch(View view, MotionEvent event) {
			x = event.getRawX();
			y = event.getRawY();
			if (y - mTouchStartY + imageWH > phoneHeight) {
				y = phoneHeight - imageWH + mTouchStartY;
			}
			if (x - mTouchStartX + imageWH > phoneWidth) {
				x = phoneWidth - imageWH + mTouchStartX;
			}
			switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					mTouchStartX = event.getX();
					mTouchStartY = event.getY();
					break;
				case MotionEvent.ACTION_MOVE:
					endX = (int) (x - mTouchStartX);
					endY = (int) (y - mTouchStartY - top);
					updateViewPosition(); // 更新浮动窗口位置参数
					break;
				case MotionEvent.ACTION_UP:
					if (endX == lastEndX && endY == lastEndY) {
						if (clicklistener != null) {
							clicklistener.click();
						}
					}
					startAnim();
					break;
				default:
					break;
			}
			return true;
		}
	}

}
